<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script type="text/javascript" src="/layui/layui.js"></script>
    <style>
        .login-form {
            position: absolute;
            width: 100%;
            padding: 20px 25px;
            box-sizing: border-box;
        }
    </style>
    <title>Params setting</title>
</head>
<body>
<div class="layui-container">
    <div>
        <form class="layui-form login-form" lay-filter="data-form" action="/updateCrawler">
            <div class="layui-form-item">
                <label class="layui-form-label">URL</label>
                <div class="layui-input-block">
                    <input type="text" name="url" placeholder="url" autocomplete="off" class="layui-input" required
                           lay-verify="url"
                           value="${crawler.url!}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">Method</label>
                <div class="layui-input-block">
                    <#--                    <input type="text" name="method" placeholder="method" autocomplete="off" class="layui-input"-->
                    <#--                           required lay-verify="hm"-->
                    <#--                           value="${crawler.method!}">-->
                    <select name="method" lay-verify="hm" required>
                        <#if crawler.method == 'get'>
                            <option value="get" selected>get</option>
                        <#else >
                            <option value="get">get</option>
                        </#if>
                        <#if crawler.method == 'put'>
                            <option value="put" selected>put</option>
                        <#else >
                            <option value="put">put</option>
                        </#if>
                        <#if crawler.method == 'delete'>
                            <option value="delete" selected>delete</option>
                        <#else >
                            <option value="delete">delete</option>
                        </#if>
                        <#if crawler.method == 'post'>
                            <option value="post" selected>post</option>
                        <#else >
                            <option value="post">post</option>
                        </#if>

                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">请求参数</label>
                <div class="layui-input-block">
            <textarea type="text" name="params" placeholder="params" autocomplete="off"
                      class="layui-textarea" required lay-verify="jsonStr">${crawler.params!}</textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">请求头</label>
                <div class="layui-input-block">
            <textarea type="text" name="headers" placeholder="headers" autocomplete="off"
                      class="layui-textarea" required lay-verify="jsonStr">${crawler.headers!}</textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="data-form" id="sub">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>

        </form>
    </div>

</div>
</body>
<script type="text/javascript">
    //Demo
    layui.use('form', function () {
        const form = layui.form;
        const $ = layui.$;
        const methodArray = ['post', 'get', 'put', 'delete'];

        form.verify({
            jsonStr: function (value, item) { //value：表单的值、item：表单的DOM对象
                // console.log(value)
                if (value !== null && value.length > 0) {
                    try {
                        JSON.parse(value)
                    } catch (e) {
                        return '不是合法的json字符串';
                    }
                }
            },
            hm: function (value, item) {
                // console.log(value);

                let result = false;
                methodArray.forEach(x => {
                    if (value === x) {
                        result = true;
                    }
                })
                if (!result) {
                    return '不是合法的method类型';
                }
            }
        });

        //监听提交
        form.on('submit(data-form)', function (data) {
            let id = ${crawler.id};
            data.field.id = id;
            fetch("/paramsSetting/update", {
                method: 'PUT',
                body: JSON.stringify(data.field)
            }).then(res => res.json()).then(json => {
                if (json.code !== 0) {
                    layer.msg('请求失败', {icon: 5});
                }
                layer.msg("修改成功", {icon: 6})
                setTimeout(function () {
                    let index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                }, 1000)

            }).catch(err => {
                console.log("error", err);
            })
            return false;
        });

        document.onkeydown = function (e) {
            if (e.keyCode === 13 || e.which === 13) {
                $("#sub").click()
            }
        }
    });
</script>
</html>